// subPages/mine/myDevice/myDevice.js
const app = getApp()
let pages = getCurrentPages();
let currPage = pages[pages.length-1];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bloodSugarId: '绑定设备',
    bloodPressureId: '绑定设备',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.IsHavaDevice();
  },

  IsHavaDevice:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/hardware',
      data: {
        userId: app.globalData.userId
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          var deviceList = res.data.data
          for(var i in deviceList)
          {
            if(deviceList[i].type == 1)
            {
              that.setData({
                bloodSugarId: 'ID:' + deviceList[i].eId
              });
            }
            if(deviceList[i].type == 2)
            {
              that.setData({//将携带的参数赋值
                bloodPressureId: 'ID:' + deviceList[i].eId
              });
            }
          }
        }
      },
      fail: function (res) {
        console.log("失败",res);
      }
    })
  },

  //体脂秤
  bodyFat:function(){
    wx.navigateTo({
      url: '/subPages/bodyFat/index/index'
    })
  },

  //血糖仪
  bloodSugar:function(e){
    var that = this
    if(that.data.bloodSugarId == '绑定设备'){
      wx.navigateTo({
        url: '../bindEquipment/bindEquipment?type=' + e.currentTarget.dataset.type
      })
    }else{
      wx.navigateTo({
        url: '/subPages/glucometer/index/index'
      })
    }
  },

  //血压计
  bloodPressure:function(e){
    var that = this
    if(that.data.bloodPressureId == '绑定设备'){
      wx.navigateTo({
        url: '../bindEquipment/bindEquipment?type=' + e.currentTarget.dataset.type
      })
    }else{
      wx.navigateTo({
        url: '/subPages/pressure/index/index'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(currPage!= 'undefined')
    {
      if (currPage.data.bloodSugarId){
        that.setData({//将携带的参数赋值
          bloodSugarId: currPage.data.bloodSugarId
        });
      }
      if (currPage.data.bloodPressureId){
        that.setData({//将携带的参数赋值
          bloodPressureId: currPage.data.bloodPressureId
        });
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})